package com.lvmama.pangolin.timer.processor;

import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.entity.EarlyWarning;
import com.lvmama.pangolin.service.EarlyWarningService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.SpringContextUtil;
import com.lvmama.pangolin.util.WeixinUtil;


public class WarnMonitorSender extends CommonJob 
{

	@Autowired
	private EarlyWarningService earlyWarningService;
	
	private Logger logger = Logger.getLogger(WarnMonitorSender.class);
	
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		logger.info("检测发送告警开始");
		int count = 0;
		try
		{
			EarlyWarning earlyWarn = new EarlyWarning();
			earlyWarn.setStatus("0");
			if (earlyWarningService == null)
			{
				earlyWarningService = (EarlyWarningService) SpringContextUtil
						.getBean(EarlyWarningService.class);
			}
			List<EarlyWarning> list = earlyWarningService.queryEarlyWarning(earlyWarn);
			if(list != null && !list.isEmpty())
			{
				for (EarlyWarning earlyWarning : list)
				{
					String content = earlyWarning.getWarning_content();
					WeixinUtil.sendWchat(parameter,content);
					count ++;
					earlyWarning.setStatus("1");
					earlyWarning.setRemark("告警发送成功!");
					earlyWarningService.updateEarlyWarningStatus(earlyWarning);
					
				}
				
			}
		}
		catch (Exception e)
		{
			logger.info("检测发送告警异常!",e);
		}
		logger.info("检测发送告警结束! 发送告警数量： " + count);
	}
	
}
